import { useDecodeParseLiveResultHook } from '@/hooks/proto/parse-live-hook';
import { useDecodeLiveViewersHook } from '@/hooks/proto/live-viewers-hook';
import { useDecodeLiveCharHook } from '@/hooks/proto/live-char-hook';
import { useDecodeLiveActionHook } from '@/hooks/proto/live-action-hook';
import { ElNotification } from 'element-plus';
import { reactive, ref } from 'vue';

export interface SpeakList {
  nickName: string;
  chatMessage: string;
}

export interface SystemList {
  nickName: string;
  actionMessage: 'enter' | 'favorite';
}

export interface LiveInfo {
  viewers: number;
  speakList: SpeakList[];
  systemList: SystemList[];
}

export const liveInfo: LiveInfo = reactive({
  viewers: 0, // 观看人数
  speakList: [], // 发言列表
  systemList: [] // 系统消息（进入、关注直播）
});

export const code = ref<number | null>();

export const useLiveStatusHook = async ({ type, data }: { type: number; data: Uint8Array }) => {
  switch (type) {
    case 2001:
      const { errCode, errMsg } = (await useDecodeParseLiveResultHook({ uint8Array: data })) as never;
      code.value = Number(errCode);
      if (code.value < 0) {
        ElNotification({
          title: `解析错误码: ${code.value}`,
          message: errMsg,
          type: 'error'
        });
        return;
      } else {
        ElNotification({
          title: `解析直播成功`,
          type: 'success'
        });
      }
      break;
    case 2002:
      const speakData = (await useDecodeLiveCharHook({ uint8Array: data })) as never;
      liveInfo.speakList.push(speakData);
      break;
    case 2003:
      const systemData = (await useDecodeLiveActionHook({ uint8Array: data })) as never;
      liveInfo.systemList.push(systemData);
      break;
    case 2004:
      const { viewers } = (await useDecodeLiveViewersHook({ uint8Array: data })) as never;
      liveInfo.viewers = viewers;
      break;
  }
};
